import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:liuxlcommon/lget/lget.dart';
import 'package:liuxlcommon/view/lxl_widget.dart';
import 'package:liuxlcommon/view/my_box.dart';
import 'package:love_newf/app/com/base/base_state.dart';
import 'package:love_newf/app/com/model/live_result_plugin_model.dart';
import 'package:love_newf/app/com/model/room_info_model.dart';
import 'package:love_newf/app/com/model/room_type.dart';
import 'package:love_newf/app/com/plugin/message_plugin.dart';
import 'package:love_newf/app/com/service/live_service.dart';
import 'package:love_newf/app/page/live/child/child_nine_view.dart';
import 'package:love_newf/app/page/live/child/child_seven_view.dart';
import 'package:love_newf/app/page/live/child/child_three_view.dart';
import 'package:love_newf/app/page/live/child/gift_prop_animation/gift_prop_animation_view.dart';
import 'package:love_newf/app/page/live/widget/live_chat_edit_view.dart';
import 'package:love_newf/value/app_const.dart';

import 'live_logic.dart';

class LivePage extends StatefulWidget {
  const LivePage({Key? key}) : super(key: key);

  @override
  State<LivePage> createState() => _LivePageState();

  ///roomId 房间id
  ///pushStreamId 邀请上麦的时候会用。
  static openPage(int roomId, {pushStreamId, off = false}) async {
    var info = await LiveService().getRoomInfo(roomId);
    if (off) {
      Get.off(const LivePage(), arguments: {"roomInfo": info});
    } else {
      Get.to(const LivePage(), arguments: {"roomInfo": info});
    }
  }
}

class _LivePageState extends BaseState<LivePage, LiveLogic> {
  @override
  get initData {
    logic.liveContext = context;
    logic.roomInfo = Get.arguments["roomInfo"];
    logic.loginRoom();
  }

  get roomType => logic.roomInfo.roomType;

  refreshMaiInfo() {
    if (logic.isRefreshMains) {
      logic.isRefreshMains = false;
      LiveMessagePlugin.refreshMaiInfo;
    }
  }

  @override
  get body => Scaffold(
          body: WillPopScope(onWillPop: () async {
        logic.exitRoomDialog();
        return !logic.isAnchor;
      }, child: GetBuilder<LiveLogic>(builder: (l) {
        WidgetsBinding.instance.addPostFrameCallback((_) => refreshMaiInfo());

        StatelessWidget content;
        if ([RoomType.THREE, RoomType.EXCLUSIVE].contains(roomType)) {
          content = ChildThreePage();
        } else if ([
          RoomType.ANGEL,
          RoomType.SEVEN,
          RoomType.SING,
          RoomType.TRAIN
        ].contains(roomType)) {
          content = ChildSevenPage();
        } else {
          content = ChildNinePage();
        }

        return LStack([
          LContainer(child: content)
              .padding(
                  top: LGet.startBarHeight, bottom: LGet.bottomPadding(context))
              .bgImg(RoomType.liveBg(logic.roomInfo.roomType)),
          const GiftPropAnimationPage(),
          logic.showChatEdit ? const LiveChatEditView() : const SizedBox()
        ]);
      })));

  @override
  Widget get easyBody => const SizedBox();
}
